<?php

namespace app\controller;

use app\BaseController;
use app\message;
use app\model\OrderDeal;
use app\model\OrderDealChargeback;
use app\model\OrderEnterpriseCertificateBusiness;
use app\model\OrderEnterpriseCertificateRegister;
use app\model\OrderEnterpriseCompany;
use app\model\OrderEnterpriseExchange;
use app\model\OrderOtherCertificateBusiness;
use app\model\OrderPersonCertificateRegister;
use app\model\OrderTransactionLog;
use app\model\OrderTransactionRecord;
use app\model\SystemBusiness;
use app\model\SystemBusinessCommissionRatio;
use app\model\SystemCertificate;
use app\model\SystemSign;
use app\model\SystemTargetLib;
use app\model\User;
use app\model\UserOrganization;
use app\Request;
use app\service\Excel;
use app\service\FinanceServer;
use app\service\Power;
use app\service\RedisService;
use app\service\System;
use app\stateData;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\App;
use think\facade\Db;

class Statistics extends BaseController
{

    private $erleiCertificate_id; //二类证书
    private $yileiCertificate_id;//一类证书
    private $business_daozheng; //业务类型之倒证
    private $business_gongsizhengshu; //业务类型之公司证书

    public function __construct(App $app,SystemCertificate $certificateModel)
    {
        parent::__construct ($app);
        //赋值二类证书id
        $this->erleiCertificate_id = $certificateModel->getWhereCertificateIdModel('Ⅱ');
        $this->yileiCertificate_id = $certificateModel->getWhereCertificateIdModel('Ⅰ');

        $this->business_daozheng = '倒证';
        $this->business_gongsizhengshu = [
            '公司证书',
            '证书匹配'
        ];
    }
    /**
     * 报表中心
     */
    public function getReportForms(
        Request           $request,
        Power             $powerSer,
        User              $userModel,
        System            $systemService,
        SystemCertificate $systemCertificateModel,
        OrderEnterpriseCompany $companyModel,
        SystemTargetLib $targetLibModel,
        Excel $excel,
        UserOrganization $organizationModel,
        RedisService $redisService,
        \app\service\Statistics $statisticsSer,
        SystemSign $systemSignModel
    ){

        $param = $request->get ();
        //获取当前登陆账号管理人员数据
        $uid = $request->param ('user_login_id');
        $idArr = $powerSer->getUserDataRange ($uid);

        //默认查询当天数据
        $startDate = date('Y-m-d');
        $endDate = date('Y-m-d');
        if (!empty($param['start_date']) && !empty($param['end_date'])){
            $startDate = $param['start_date'];
            $endDate = $param['end_date'];
        }

        //获取时间区间签到天数
        $dayNum = $systemSignModel->whereBetweenTime ('create_date',$startDate,$endDate)->count ();

        //如果有部门条件
        if ($param['org']){
            $OrgId = $systemService->getOrgIdStr($param['org'],$organizationModel->getStructure ());
            $idArr = $userModel->getOidUidDataModel ($OrgId);
        }

        //如果有人员条件
        if ($param['uid']){
            $idArr = $param['uid'];
        }

        if (empty($param['page']) || empty($param['pageNum'])){
            return ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);
        }

        //查询人员基本信息
//        print_r ($idArr);die;
//        $userPageData = $userModel->getIdUserFieldRandInfo($idArr,$param['page'],$param['pageNum']);
        $userPageData = $userModel->getIdUserFieldRandInfo($idArr,'','',$startDate,$endDate);

        $allData = $userModel->getIdUserCountInfo($idArr,$startDate,$endDate);

        //ID分页
//        if (!$param['export']){ //如果导出的话 计算全部数据
//            if (count ($userAllData) > $param['pageNum']){
//                $userData = page($userAllData,$param['page'],$param['pageNum']);
//            }
//        }else{
        $userData = $userPageData;
//        }



        $data = $redisService->getStatisticsPerformance ($startDate,$endDate);

        //取用户ID用来聚合计算
        $uidIdArr = array_column ($userData,'id');

        $sum = [
            'id'=>'总计',
            'nickname'=>'',
            'department_id'=>'',
            'yjzj'=>0,
            'ylyjzj'=>0,
            'elyjzj'=>0,
            'zszc_gszs'=>0,
            'zszc_dz'=>0,
            'zszctj'=>0,
            'zsbltj'=>0,
            'qyzstj'=>0,
            'yzmb'=>0,
            'sjdc'=>0,
            'sjjd'=>0,
//            'eldcjd'=>0,
//            'dcjd'=>0,
            'yl'=>0,
            'el'=>0
        ];
        //初始化用户数据
        $userData = $statisticsSer->getUidDataPerformanceData ($userData);

        //时间区间是否包含当天
        if(strtotime ($endDate) >= strtotime (date('Y-m-d'))){
            foreach ($userData as $userDatum) {
                if(!in_array ($userDatum['id'],$uidIdArr)){
                    continue;
                }

                $sum['ylyjzj'] += $userDatum['ylyjzj'];

                $sum['elyjzj'] += $userDatum['elyjzj'];

                $sum['zszc_gszs'] += $userDatum['zszc_gszs'];

                $sum['zszc_dz'] += $userDatum['zszc_dz'];

                $sum['zszctj'] += $userDatum['zszctj'];

                $sum['zsbltj'] += $userDatum['zsbltj'];

                $sum['qyzstj'] += $userDatum['qyzstj'];

                $sum['yl'] += $userDatum['yl'];

                $sum['el'] += $userDatum['el'];

                $sum['yjzj'] += $userDatum['yjzj'];

                $sum['sjdc'] += $userDatum['sjdc'];
            }
        }else{
            foreach ($userData as &$userDatums) {

                $userDatums['ylyjzj'] = 0;
                $userDatums['elyjzj'] = 0;
                $userDatums['zszc_gszs'] = 0;
                $userDatums['zszc_dz'] = 0;
                $userDatums['zszctj'] = 0;
                $userDatums['zsbltj'] = 0;
                $userDatums['qyzstj'] = 0;
                $userDatums['yl'] = 0;
                $userDatums['el'] = 0;
                $userDatums['yzmb'] = 0;
                $userDatums['yjzj'] = 0;
                $userDatums['sjdc'] = 0;
//                $userDatums['eldcjd'] = 0;
//                $userDatums['dcjd'] = 0;

            }
        }
        /**
         * 获取当月目标
         */

        if ($data){
            foreach ($data as  $datum){
                $dayYzmu = 0; //每一天总目标合计
                foreach ($datum as $item) {
                    if(!in_array ($item['id'],$uidIdArr)){
                        continue;
                    }
                    $arrk = array_search ($item['id'], $uidIdArr, true);

                    $userData[$arrk]['ylyjzj'] += $item['ylyjzj'];
                    $sum['ylyjzj'] += $item['ylyjzj'];

                    $userData[$arrk]['elyjzj'] += $item['elyjzj'];
                    $sum['elyjzj'] += $item['elyjzj'];

                    $userData[$arrk]['zszc_gszs'] += $item['zszc_gszs'];
                    $sum['zszc_gszs'] += $item['zszc_gszs'];

                    $userData[$arrk]['zszc_dz'] += $item['zszc_dz'];
                    $sum['zszc_dz'] += $item['zszc_dz'];

                    $userData[$arrk]['zszctj'] += $item['zszctj'];
                    $sum['zszctj'] += $item['zszctj'];

                    $userData[$arrk]['zsbltj'] += $item['zsbltj'];
                    $sum['zsbltj'] += $item['zsbltj'];

                    $userData[$arrk]['qyzstj'] += $item['qyzstj'];
                    $sum['qyzstj'] += $item['qyzstj'];

                    $userData[$arrk]['yl'] += $item['yl'];
                    $sum['yl'] += $item['yl'];

                    $userData[$arrk]['el'] += $item['el'];
                    $sum['el'] += $item['el'];


                    $userData[$arrk]['yjzj'] += $item['yjzj'];
                    $sum['yjzj'] += $item['yjzj'];

                    $userData[$arrk]['sjdc'] += $item['sjdc'];
                    $sum['sjdc'] += $item['sjdc'];


                    $userData[$arrk]['yzmb'] = $targetLibModel->getValueTargetModel(date('Y-m'),$item['id'],1)?:0;


                    //二类达成进度
                    if ($userData[$arrk]['yzmb'] == 0){
                        $userData[$arrk]['eldcjd'] = 0 .'%';
                    }else{
                        $userData[$arrk]['eldcjd'] = round ($userData[$arrk]['elyjzj']/$userData[$arrk]['yzmb']*100,2). '%';
                        if ($userData[$arrk]['eldcjd'] > 100){
                            $userData[$arrk]['eldcjd'] = 100 . '%';
                        }
                        if ($userData[$arrk]['eldcjd'] < 0){
                            $userData[$arrk]['eldcjd'] = 0 . '%';
                        }
                    }

                    //达成进度
                    if ($userData[$arrk]['yzmb'] == 0){
                        $userData[$arrk]['dcjd'] = 0 .'%';
                    }else{
                        $userData[$arrk]['dcjd'] = round ($userData[$arrk]['sjdc']/$userData[$arrk]['yzmb']*100,2). '%';
                        if ($userData[$arrk]['dcjd'] > 100){
                            $userData[$arrk]['dcjd'] = 100 . '%';
                        }
                        if ($userData[$arrk]['dcjd'] < 0){
                            $userData[$arrk]['dcjd'] = 0 . '%';
                        }
                    }

                    //时间进度
                    $moon_day = $targetLibModel->getValueTargetDayModel(date('Y-m'),$item['id'],3,3)?:0;
                    if ($moon_day == 0){
                        $userData[$arrk]['sjjd'] = 0 .'%';
                        $sum['sjjd'] = 0 .'%';
                    }else{
                        $userData[$arrk]['sjjd'] = round  ($dayNum/$moon_day,2)*100 . '%';
                        $sum['sjjd'] = $userData[$arrk]['sjjd'];
                    }
                }
            }


        }
        //计算目标总计
        foreach ($userData as $datums) {
            $sum['yzmb'] += $datums['yzmb'];
        }
        //二类达成计算百分比
        if ($sum['yzmb'] > 0){
            $sum['eldcjd'] = round ($sum['elyjzj']/$sum['yzmb']*100,2). '%';
            $sum['dcjd'] = round ($sum['yjzj']/$sum['yzmb']*100,2). '%';
        }else{
            $sum['eldcjd'] = '0%';
            $sum['dcjd'] = '0%';
        }


//        $sum = [
//            'id'=>'总计',
//            'nickname'=>'',
//            'department_id'=>'',
//            'yjzj'=>0,
//            'ylyjzj'=>0,
//            'elyjzj'=>0,
//            'zszc_gszs'=>0,
//            'zszc_dz'=>0,
//            'zszctj'=>0,
//            'zsbltj'=>0,
//            'qyzstj'=>0,
//            'yzmb'=>0,
//            'sjdc'=>0,
//            'sjjd'=>0,
//            'eldcjd'=>0,
//            'dcjd'=>0,
//            'yl'=>0,
//            'el'=>0
//        ];
//        //当天
//        $toDay = date('d');
//
//        //总计
//        foreach ($userData as &$userDatum) {
//            //拼接用户部门
//            if ($userDatum['department_id']['id']){
////                $userDatum['department_id'] = '';
//                $userDatum['department_id'] = $systemService->getOrgStrName($userDatum['department_id']['id']);
//            }
//
//            //一类业绩总计
//            $userDatum['ylyjzj'] = $this->getTotal ($startDate,$endDate,$userDatum['id'],$this->yileiCertificate_id);
//            $sum['ylyjzj'] += $userDatum['ylyjzj'];
//
//            //二类业绩总计
//            $userDatum['elyjzj'] = $this->getTotal ($startDate,$endDate,$userDatum['id'],$this->erleiCertificate_id);
//            $sum['elyjzj'] += $userDatum['elyjzj'];
//            //公司证书
//            $userDatum['zszc_gszs'] = $this->getGsYj ($startDate,$endDate,$userDatum['id'],1);
//            $sum['zszc_gszs'] += $userDatum['zszc_gszs'];
//            //倒证
//            $userDatum['zszc_dz'] = $this->getGsYj ($startDate,$endDate,$userDatum['id'],2);
//            $sum['zszc_dz'] += $userDatum['zszc_dz'];
//
//            //总计
//            $userDatum['zszctj'] = $userDatum['zszc_dz']+$userDatum['zszc_gszs'];
//            $sum['zszctj'] += $userDatum['zszctj'];
//            //证书办理
//            $userDatum['zsbltj'] = $this->getGsYj ($startDate,$endDate,$userDatum['id'],3);
//            $sum['zsbltj'] += $userDatum['zsbltj'];
//            //企业证书
//            $userDatum['qyzstj'] = $this->getGsYj ($startDate,$endDate,$userDatum['id'],4);
//            $sum['qyzstj'] += $userDatum['qyzstj'];
//            //退证一类业绩
//            $chargeBack = $this->getChargrBack($startDate,$endDate,$userDatum['id'],$this->yileiCertificate_id,$this->erleiCertificate_id);
//            $userDatum['yl'] = $chargeBack['yl'];
//            $sum['yl'] += $chargeBack['yl'];
//            //退证二类业绩
//            $userDatum['el'] = $chargeBack['el'];
//            $sum['el'] += $chargeBack['el'];
//            //月总目标
//            $userDatum['yzmb'] = $targetLibModel->getValueTargetModel(date('Y-m'),$userDatum['id'],1)?:0;
//            $sum['yzmb'] += $userDatum['yzmb'];
//            //业绩总计
//            $userDatum['yjzj'] = $userDatum['qyzstj']+$userDatum['zsbltj']+$userDatum['zszctj'];
//            $sum['yjzj'] += $userDatum['yjzj'];
//
//            //实际达成
//            $userDatum['sjdc'] = $userDatum['yjzj'];
//            $sum['sjdc'] = $sum['yjzj'];
//
//            $moon_day = $targetLibModel->getValueTargetDayModel(date('Y-m'),$userDatum['id'],1)?:0;
//            if ($moon_day == 0){
//                $userDatum['sjjd'] = 0 .'%';
//            }else{
//                $userDatum['sjjd'] = round  ($toDay/$moon_day,2)*100 . '%';
//            }
//            //二类达成进度
//            if ($userDatum['yzmb'] == 0){
//                $userDatum['eldcjd'] = 0 .'%';
//            }else{
//                $userDatum['eldcjd'] = round ($userDatum['elyjzj']/$userDatum['yzmb']*100,2). '%';
//                if ($userDatum['eldcjd'] > 100){
//                    $userDatum['eldcjd'] = 100 . '%';
//                }
//                if ($userDatum['eldcjd'] < 0){
//                    $userDatum['eldcjd'] = 0 . '%';
//                }
//            }
//            //总计二类达成进度
//            if ($sum['yzmb'] == 0){
//                $sum['eldcjd'] = 0 .'%';
//            }else{
//                $sum['eldcjd'] = round ($sum['elyjzj']/$sum['yzmb']*100,2). '%';
//                if ($sum['eldcjd'] > 100){
//                    $sum['eldcjd'] = 100 . '%';
//                }
//                if ($sum['eldcjd'] < 0){
//                    $sum['eldcjd'] = 0 . '%';
//                }
//            }
//            //达成进度
//            if ($userDatum['yzmb'] == 0){
//                $userDatum['dcjd'] = 0 .'%';
//            }else{
//                $userDatum['dcjd'] = round ($userDatum['sjdc']/$userDatum['yzmb']*100,2). '%';
//                if ($userDatum['dcjd'] > 100){
//                    $userDatum['dcjd'] = 100 . '%';
//                }
//                if ($userDatum['dcjd'] < 0){
//                    $userDatum['dcjd'] = 0 . '%';
//                }
//            }
//            //总计总达成进度
//            if ($sum['yzmb'] == 0){
//                $sum['dcjd'] = 0 .'%';
//            }else{
//                $sum['dcjd'] = round ($sum['sjdc']/$sum['yzmb']*100,2). '%';
//                if ($sum['dcjd'] > 100){
//                    $sum['dcjd'] = 100 . '%';
//                }
//                if ($sum['dcjd'] < 0){
//                    $sum['dcjd'] = 0 . '%';
//                }
//            }
////            echo 1;die;
//        }

        //总计
        if ($param['exports']){
            $header = [
                ['s_cell' => 'id', 's_value' => '序号', 's_width' => 10],
                ['s_cell' => 'department_id', 's_value' => '公司部门', 's_width' => 10],
                ['s_cell' => 'nickname', 's_value' => '人员姓名', 's_width' => 10],
                ['s_cell' => 'yjzj', 's_value' => '业绩总数', 's_width' => 10],
                ['s_cell' => 'ylyjzj', 's_value' => '一类业绩总计', 's_width' => 10],
                ['s_cell' => 'elyjzj', 's_value' => '二类业绩总计', 's_width' => 10],
                ['s_cell' => 'zszc_gszs', 's_value' => '公司证书', 's_width' => 10],
                ['s_cell' => 'zszc_dz', 's_value' => '外部证书', 's_width' => 10],
                ['s_cell' => 'zszctj', 's_value' => '总计', 's_width' => 10],
                ['s_cell' => 'zsbltj', 's_value' => '证书办理', 's_width' => 10],
                ['s_cell' => 'qyzstj', 's_value' => '企业证书', 's_width' => 10],
                ['s_cell' => 'yl', 's_value' => '退证一类业绩', 's_width' => 10],
                ['s_cell' => 'el', 's_value' => '退证二类业绩', 's_width' => 10],
                ['s_cell' => 'yzmb', 's_value' => '月总目标', 's_width' => 10],
                ['s_cell' => 'sjdc', 's_value' => '实际达成', 's_width' => 10],
                ['s_cell' => 'sjjd', 's_value' => '月时间进度', 's_width' => 10],
                ['s_cell' => 'eldcjd', 's_value' => '二类达成进度', 's_width' => 10],
                ['s_cell' => 'dcjd', 's_value' => '总达成进度', 's_width' => 10],

            ];
            $excel->outExcelStart($userData, $header, '报表中心导出');
            die;
        }


        $datas= page ($userData,$param['page'],$param['pageNum'],'','',false);

        $result = array_merge ($datas,[$sum]);

        return ReJson (
            message::$SUCC_CODE,
            message::$SUCC_SELECT_DATA,
            $result,
            ['total'=>$allData,'pageNum'=>$param['pageNum']]
        );
    }



    /**
     * 报表中心-证书注册-统计
     */
    public function zszcStatistics(
        Request          $request,
        Power            $powerService,
        System           $systemService,
        UserOrganization $organizationModel,
        User             $userModel,
        Excel            $excel,
        RedisService $redisService
    ){
        $param = $request->get ();
        //获取当前登陆账号管理人员数据
        $uid = $request->param ('user_login_id');
        $idArr = $powerService->getUserDataRange ($uid);

        //如果有部门条件
        if ($param['org']){
            $OrgId = $systemService->getOrgIdStr($param['org'],$organizationModel->getStructure ());
            $idArr = $userModel->getOidUidDataModel ($OrgId);
        }

        //如果有人员条件
        if ($param['uid']){
            $idArr = $param['uid'];
        }

        //默认查询当天数据
        $startDate = date('Y-m-d').' 00:00:00';
        $endDate = date('Y-m-d').' 23:59:59';
        if (!empty($param['start_date']) && !empty($param['end_date'])){
            $startDate = $param['start_date'].' 00:00:00';
            $endDate = $param['end_date'].' 23:59:59';
        }

        if (empty($param['page']) || empty($param['pageNum'])){
            return ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);
        }

        //查询人员基本信息
        $userData = $userModel->getIdUserFieldRandInfo($idArr,'','',$startDate,$endDate);
        $allData = $userModel->getIdUserCountInfo($idArr,$startDate,$endDate);

        //公司证书
        $orderEnterPriseCompanyModel = new OrderEnterpriseCompany();
        //倒证
        $orderEnterPriseExchangeModel = new OrderEnterpriseExchange();

        //所有已售出的成交记录
//        $orderDealModel = new OrderDeal();
//        $data = $orderDealModel->getDateCerIdDataModel($startDate,$endDate);
        /**
         * 获取证书注册-新出、续出、续签   订单数量|订单业绩
         */
        $totalData['department_id'] = ['id'=>'','text'=>'总计'];
        $totalData['new_order_num'] = 0;
        $totalData['new_order_price'] = 0;
        $totalData['xc_order_num'] = 0;
        $totalData['xc_order_price'] = 0;
        $totalData['xq_order_num'] = 0;
        $totalData['xq_order_price'] = 0;
        $totalData['all_order_num'] = 0;
        $totalData['all_order_price'] = 0;
        foreach ($userData as $k => &$userDatum) {
            //初始化

            $userDatum['new_order_num'] = 0;
            $userDatum['new_order_price'] = 0;
            $userDatum['xc_order_num'] = 0;
            $userDatum['xc_order_price'] = 0;
            $userDatum['xq_order_num'] = 0;
            $userDatum['xq_order_price'] = 0;
            $userDatum['all_order_num'] = 0;
            $userDatum['all_order_price'] = 0;

//            foreach ($data as $datum) {
//                if ($datum['status'] == 1){
//                    //获取新出订单量、业绩
//                    $orderInfo = $orderEnterPriseCompanyModel->getUidCertificateCodeYhModel ($userDatum['id'],$datum['certificate_code'],$startDate,$endDate);
//                    $exOrderInfo = $orderEnterPriseExchangeModel->getUidCerDateDataYhModel ($userDatum['id'],$datum['certificate_code'],$startDate,$endDate);
//
//                    if ($orderInfo){
//                        $userDatum['new_order_num'] += $orderInfo['order_num'];
//                        $userDatum['new_order_price'] += $orderInfo['order_price'];
//                    }
//                    if ($exOrderInfo){
//                        $userDatum['new_order_num'] += $exOrderInfo['order_num'];
//                        $userDatum['new_order_price'] += $exOrderInfo['order_price'];
//                    }
//                }elseif ($datum['status'] == 2){
//                    //获取新出订单量、业绩
//                    $orderInfo = $orderEnterPriseCompanyModel->getUidCertificateCodeYhModel ($userDatum['id'],$datum['certificate_code'],$startDate,$endDate);
//                    $exOrderInfo = $orderEnterPriseExchangeModel->getUidCerDateDataYhModel ($userDatum['id'],$datum['certificate_code'],$startDate,$endDate);
//
//                    if ($orderInfo){
//                        $userDatum['xq_order_num'] += $orderInfo['order_num'];
//                        $userDatum['xq_order_price'] += $orderInfo['order_price'];
//                    }
//                    if ($exOrderInfo){
//                        $userDatum['xq_order_num'] += $exOrderInfo['order_num'];
//                        $userDatum['xq_order_price'] += $exOrderInfo['order_price'];
//                    }
//                }elseif($datum['status'] == 3){
//                    //获取新出订单量、业绩
//                    $orderInfo = $orderEnterPriseCompanyModel->getUidCertificateCodeYhModel ($userDatum['id'],$datum['certificate_code'],$startDate,$endDate);
//                    $exOrderInfo = $orderEnterPriseExchangeModel->getUidCerDateDataYhModel ($userDatum['id'],$datum['certificate_code'],$startDate,$endDate);
//
//                    if ($orderInfo){
//                        $userDatum['xc_order_num'] += $orderInfo['order_num'];
//                        $userDatum['xc_order_price'] += $orderInfo['order_price'];
//                    }
//                    if ($exOrderInfo){
//                        $userDatum['xc_order_num'] += $exOrderInfo['order_num'];
//                        $userDatum['xc_order_price'] += $exOrderInfo['order_price'];
//                    }
//                }
//            }

//            //获取新出订单量、业绩
//            $orderInfo = $orderEnterPriseCompanyModel->getUidCertificateCodeModel ($userDatum['id'],1,$startDate,$endDate);
//            $exOrderInfo = $orderEnterPriseExchangeModel->getUidCerDateDataModel ($userDatum['id'],1,$startDate,$endDate);
//
//            if ($orderInfo){
//                $userDatum['new_order_num'] += $orderInfo['order_num'];
//                $userDatum['new_order_price'] += $orderInfo['order_price'];
//            }
//            if ($exOrderInfo) {
//                $userDatum['new_order_num'] += $exOrderInfo['order_num'];
//                $userDatum['new_order_price'] += $exOrderInfo['order_price'];
//            }
////print_r ($userDatum);die;
//            //获取续签订单量、业绩
//            $orderInfos = $orderEnterPriseCompanyModel->getUidCertificateCodeModel ($userDatum['id'],2,$startDate,$endDate);
//            $exOrderInfos = $orderEnterPriseExchangeModel->getUidCerDateDataModel ($userDatum['id'],2,$startDate,$endDate);
//            if ($orderInfo){
//                $userDatum['xq_order_num'] += $orderInfos['order_num'];
//                $userDatum['xq_order_price'] += $orderInfos['order_price'];
//            }
//            if ($exOrderInfo){
//                $userDatum['xq_order_num'] += $exOrderInfos['order_num'];
//                $userDatum['xq_order_price'] += $exOrderInfos['order_price'];
//            }
//            //获取续出订单量、业绩
//            $orderInfoss = $orderEnterPriseCompanyModel->getUidCertificateCodeModel ($userDatum['id'],3,$startDate,$endDate);
//            $exOrderInfoss = $orderEnterPriseExchangeModel->getUidCerDateDataModel ($userDatum['id'],3,$startDate,$endDate);
//            if ($orderInfo){
//                $userDatum['xc_order_num'] += $orderInfoss['order_num'];
//                $userDatum['xc_order_price'] += $orderInfoss['order_price'];
//            }
//            if ($exOrderInfo){
//                $userDatum['xc_order_num'] += $exOrderInfoss['order_num'];
//                $userDatum['xc_order_price'] += $exOrderInfoss['order_price'];
//            }

            //获取新出订单量、业绩
            $orderInfo = $orderEnterPriseCompanyModel->getUidCertificateCodeModel ($userDatum['id'],1,$startDate,$endDate);

            $exOrderInfo = $orderEnterPriseExchangeModel->getUidCerDateDataModel ($userDatum['id'],1,$startDate,$endDate);

            if ($orderInfo){
                $userDatum['new_order_num'] += $orderInfo['new_order_num'];
                $totalData['new_order_num'] += $orderInfo['new_order_num'];

                $userDatum['new_order_price'] += $orderInfo['new_order_price'];
                $totalData['new_order_price'] += $orderInfo['new_order_price'];

                $userDatum['xq_order_num'] += $orderInfo['xq_order_num'];
                $totalData['xq_order_num'] += $orderInfo['xq_order_num'];

                $userDatum['xq_order_price'] += $orderInfo['xq_order_price'];
                $totalData['xq_order_price'] += $orderInfo['xq_order_price'];

                $userDatum['xc_order_num'] += $orderInfo['xc_order_num'];
                $totalData['xc_order_num'] += $orderInfo['xc_order_num'];

                $userDatum['xc_order_price'] += $orderInfo['xc_order_price'];
                $totalData['xc_order_price'] += $orderInfo['xc_order_price'];

            }
            if ($exOrderInfo) {
                $userDatum['new_order_num'] += $exOrderInfo['new_order_num'];
                $totalData['new_order_num'] += $exOrderInfo['new_order_num'];

                $userDatum['new_order_price'] += $exOrderInfo['new_order_price'];
                $totalData['new_order_price'] += $exOrderInfo['new_order_price'];


                $userDatum['xq_order_num'] += $exOrderInfo['xq_order_num'];
                $totalData['xq_order_num'] += $exOrderInfo['xq_order_num'];

                $userDatum['xq_order_price'] += $exOrderInfo['xq_order_price'];
                $totalData['xq_order_price'] += $exOrderInfo['xq_order_price'];

                $userDatum['xc_order_num'] += $exOrderInfo['xc_order_num'];
                $totalData['xc_order_num'] += $exOrderInfo['xc_order_num'];

                $userDatum['xc_order_price'] += $exOrderInfo['xc_order_price'];
                $totalData['xc_order_price'] += $exOrderInfo['xc_order_price'];

            }
            //总计
            $userDatum['all_order_num'] = $userDatum['new_order_num'] + $userDatum['xq_order_num'] + $userDatum['xc_order_num'];
            $totalData['all_order_num'] += $userDatum['new_order_num'] + $userDatum['xq_order_num'] + $userDatum['xc_order_num'];

            $userDatum['all_order_price'] = $userDatum['new_order_price'] + $userDatum['xq_order_price'] + $userDatum['xc_order_price'];
            $totalData['all_order_price'] += $userDatum['new_order_price'] + $userDatum['xq_order_price'] + $userDatum['xc_order_price'];

        }

        //总计
        if ($param['exports']){
            $header = [
                ['s_cell' => 'id', 's_value' => '序号', 's_width' => 10],
                ['s_cell' => 'department_id', 's_value' => '公司部门', 's_width' => 10],
                ['s_cell' => 'nickname', 's_value' => '人员姓名', 's_width' => 10],
                ['s_cell' => 'new_order_num', 's_value' => '新出单量', 's_width' => 10],
                ['s_cell' => 'new_order_price', 's_value' => '新出业绩', 's_width' => 10],
                ['s_cell' => 'xc_order_num', 's_value' => '续出单量', 's_width' => 10],
                ['s_cell' => 'xc_order_price', 's_value' => '续出业绩', 's_width' => 10],
                ['s_cell' => 'xq_order_num', 's_value' => '续签单量', 's_width' => 10],
                ['s_cell' => 'xq_order_price', 's_value' => '续签业绩', 's_width' => 10],
                ['s_cell' => 'all_order_num', 's_value' => '单量总计', 's_width' => 10],
                ['s_cell' => 'all_order_price', 's_value' => '业绩总计', 's_width' => 10]
            ];

            $excel->outExcelStart($userData, $header, '证书注册统计导出');
            die;
        }

        $pageData = page ($userData,$param['page'],$param['pageNum'],'','',false);

        $pageData[] = $totalData;
        return ReJson (
            message::$SUCC_CODE,
            message::$SUCC_SELECT_DATA,
            $pageData,
            [
                'total'=>$allData,
                'pageNum'=>$param['pageNum']
            ]
        );
    }

    /**
     * 报表中心-证书注册占比统计
     */
//    public function getZszcProportionStatistics(
//        Request          $request,
//        Power            $powerService,
//        System           $systemService,
//        UserOrganization $organizationModel,
//        User             $userModel,
//        Excel $excel
//    ){
//        $param = $request->get ();
//        //获取当前登陆账号管理人员数据
//        $uid = $request->param ('user_login_id');
//        $idArr = $powerService->getUserDataRange ($uid);
//
//        //如果有部门条件
//        if ($param['org']){
//            $OrgId = $systemService->getOrgIdStr($param['org'],$organizationModel->getStructure ());
//            $idArr = $userModel->getOidUidDataModel ($OrgId);
//        }
//
//        //如果有人员条件
//        if ($param['uid']){
//            $idArr = [$param['uid']];
//        }
//
//        //默认查询当天数据
//        $startDate = date('Y-m-d').' 00:00:00';
//        $endDate = date('Y-m-d').' 23:59:59';
//        if (!empty($param['start_date']) && !empty($param['end_date'])){
//            $startDate = $param['start_date'].' 00:00:00';
//            $endDate = $param['end_date'].' 23:59:59';
//        }
//
//        //查询默认条件分类
//        //1.证书类别、等级  2.周期  3.B证  4.社保
//        //  / 或条件  、与条件
//        $whereDate = [
//            [
//                1=>'Ⅱ/建造师/一级', 2=>'6个月以内',3=>'',4=>''
//            ],
//            [
//                1=>'Ⅱ/建造师/一级', 2=>'6个月以上',3=>'',4=>''
//            ],
//            [
//                1=>'Ⅱ/建造师/一级', 2=>'三个月',3=>'',4=>''
//            ],
//
//            [
//                1=>'Ⅱ/建造师/二级', 2=>'',3=>'',4=>'灵活社保'
//            ],
//            [
//                1=>'Ⅱ/建造师/二级', 2=>'长期证',3=>'交安B/水安B/建安B',4=>''
//            ],
//            [
//                1=>'Ⅱ/建造师/二级', 2=>'三个月',3=>'交安B/水安B/建安B',4=>''
//            ],
//
//            [
//                1=>'Ⅱ/职称', 2=>'',3=>'',4=>''
//            ],
//            [
//                1=>'Ⅱ/注册工程师', 2=>'',3=>'',4=>''
//            ],
//            [
//                1=>'Ⅱ/八大员', 2=>'',3=>'',4=>''
//            ],
//            [
//                1=>'Ⅱ/三类人员', 2=>'',3=>'',4=>''
//            ],
//        ];
//        $excelWhereDate = [
//            [
//                1=>'一级建造师', 2=>'6个月以内',3=>'',4=>''
//            ],
//            [
//                1=>'一级建造师', 2=>'6个月以上',3=>'',4=>''
//            ],
//            [
//                1=>'一级建造师', 2=>'裸证三个月',3=>'',4=>''
//            ],
//
//            [
//                1=>'二级建造师', 2=>'',3=>'',4=>'灵活社保'
//            ],
//            [
//                1=>'二级建造师', 2=>'带B长期证',3=>'',4=>''
//            ],
//            [
//                1=>'二级建造师', 2=>'带B三个月',3=>'',4=>''
//            ],
//
//            [
//                1=>'职称', 2=>'',3=>'',4=>''
//            ],
//            [
//                1=>'注册工程师', 2=>'',3=>'',4=>''
//            ],
//            [
//                1=>'八大员', 2=>'',3=>'',4=>''
//            ],
//            [
//                1=>'三类人员', 2=>'',3=>'',4=>''
//            ],
//        ];
//
//        //查询公司证书
//        $orderEnterPriseCompanyModel = new OrderEnterpriseCompany();
//        $orderEnterPriseExchangeModel = new OrderEnterpriseExchange();
//        $systemCertificateModel = new SystemCertificate();
//
//        $gszs = []; //公司证书结果集合
//        $dz = [];   //倒证结果集合
//
//        //查询总业绩
//        $wheres = 'certificate_leixing ='.$systemCertificateModel->getWhereCertificateIdModel('Ⅱ');
//        $gszsAll = $orderEnterPriseCompanyModel->getWhereModel ($wheres,$idArr,$startDate,$endDate); //公司证书查询
//        $dzAll =  $orderEnterPriseExchangeModel->getWhereModel($wheres,$idArr,$startDate,$endDate);//倒证查询
//
//        foreach ($whereDate as &$item) {
//            $where = '';
//            foreach ($item as $k =>$itemTam) {
//
//                //查询是否是多条条件/  、
//                if (!$itemTam){
//                    continue;
//                }
//
//                //包含或条件 OR
//                if ($k == 1){
//                    $iArrData = explode ('/',$itemTam);
//
//                    //查证书
//                    if(count ($iArrData) == 3){
//                        $where = 'certificate_leixing = '.$systemCertificateModel->getWhereCertificateIdModel($iArrData[0]);
//                        $where .= ' and certificate_leibie = '.($systemCertificateModel->getWhereCertificateIdModel($iArrData[1],$systemCertificateModel->getWhereCertificateIdModel($iArrData[0]))?:0);
//                        $where .= ' and certificate_dengji = '.($systemCertificateModel->getWhereCertificateIdModel($iArrData[2],$systemCertificateModel->getWhereCertificateIdModel($iArrData[1]))?:0);
//                    }else{
//                        $where = 'certificate_leixing = '.$systemCertificateModel->getWhereCertificateIdModel($iArrData[0]);
//                        $where .= ' and certificate_leibie = '.($systemCertificateModel->getWhereCertificateIdModel($iArrData[1],$systemCertificateModel->getWhereCertificateIdModel($iArrData[0]))?:0);
//                    }
//
//                }elseif ($k == 2){
//                    //查周期
//                    if ($itemTam === '6个月以内'){
//                        $where .= ' and cycle <= '.array_search('6个月', stateData::$_cycle);
//                    }else if($itemTam === '6个月以上'){
//                        $where .= ' and cycle > '.array_search('6个月', stateData::$_cycle);
//                    }else if($itemTam === '长期证'){
//                        $where .= ' and cycle >= '.array_search('3个月', stateData::$_cycle);
//                    }else{
//                        $where .= ' and cycle <= '.array_search('3个月', stateData::$_cycle);
//                    }
//
//                }elseif($k == 3){
//                    //查B证
//                    $where .= ' and certificate_b <> 999';
//                }else{
//                    //查社保
//                    $where .= ' and social_security = 3';
//                }
//            }
//
////print_r ($where);
////            echo  PHP_EOL;
//            $gszsData = $orderEnterPriseCompanyModel->getWhereModel ($where,$idArr,$startDate,$endDate); //公司证书查询
//            $dzData =  $orderEnterPriseExchangeModel->getWhereModel($where,$idArr,$startDate,$endDate);//倒证查询
//
//            //拼接总业绩
//            $gszsData['order_all_price'] = $dzData['order_all_price'] = $gszsAll['order_price'] + $dzAll['order_price'];
//
//            //拼接业绩占比
//            $gszsData['order_performance_zb'] = number_format ($gszsData['order_price'] / ($gszsData['order_all_price']?:1),2)*100 .'%';
//            $dzData['order_performance_zb'] = number_format ($dzData['order_price'] / ($dzData['order_all_price']?:1),2)*100 .'%';
//
//            $gszs[] = $gszsData;
//            $dz[] = $dzData;
//
//        }
//        $result = array_merge ($gszs,$dz);
//
//        //导出
//        if ($param['exports']){
//            foreach ($result as $k => &$resultDatum) {
//                if ($k >= 10){
//                    $resultDatum['type'] = '外部证书';
//                    $k -= 10;
//                }else{
//                    $resultDatum['type'] = '公司证书';
//                }
//                $resultDatum['cer_type'] = $excelWhereDate[$k][1];
//                $resultDatum['other_type'] = '';
//                $resultDatum['other_type'] .= $excelWhereDate[$k][2] ? $excelWhereDate[$k][2].'/': '';
//                $resultDatum['other_type'] .= $excelWhereDate[$k][3] ? $excelWhereDate[$k][3].'/': '';
//                $resultDatum['other_type'] .= $excelWhereDate[$k][4] ?? '';
//            }
//
//
//            $header = [
////                ['s_cell' => 'id', 's_value' => '序号', 's_width' => 10],
//                ['s_cell' => 'type', 's_value' => '', 's_width' => 10],
//                ['s_cell' => 'cer_type', 's_value' => '类别', 's_width' => 10],
//                ['s_cell' => 'other_type', 's_value' => '周期/B证/社保', 's_width' => 10],
//                ['s_cell' => 'order_num', 's_value' => '单量', 's_width' => 10],
//                ['s_cell' => 'order_price', 's_value' => '业绩', 's_width' => 10],
//                ['s_cell' => 'order_all_price', 's_value' => '总业绩', 's_width' => 10],
//                ['s_cell' => 'order_performance_zb', 's_value' => '业绩占比', 's_width' => 10],
//            ];
//
//            $data = array_values ($result);
//            $spreadsheet = new Spreadsheet();
//
//            $spreadsheet->getActiveSheet()->mergeCells("A2:A11");
//            $spreadsheet->getActiveSheet()->mergeCells("B2:B4");
//            $spreadsheet->getActiveSheet()->mergeCells("B5:B7");
//
//            $spreadsheet->getActiveSheet()->mergeCells("A12:A21");
//            $spreadsheet->getActiveSheet()->mergeCells("B12:B14");
//            $spreadsheet->getActiveSheet()->mergeCells("B15:B17");
//
//            $sheet = $spreadsheet->getActiveSheet();
//            $sheet->getStyle('A2:A11')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
//            $sheet->getStyle('B2:B4')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
//            $sheet->getStyle('B5:B7')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
//            $sheet->getStyle('A12:A21')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
//            $sheet->getStyle('B12:B14')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
//            $sheet->getStyle('B15:B17')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
//            //设置excel格式
//            $char_index = [];
//            for ($x = 'A'; $x < 'ZZ'; $x++) {
//                array_push($char_index,$x);
//            }
//            $letter = '';
//            //设置表头
//            foreach ($header as $i => $iValue) {
//                //设置字母
//                $letter = $this->getLetter ($i);
//                $sheet->setCellValue ("{$char_index[$i]}1", $iValue['s_value']);
//
//                // 设置 A 列 列宽
////            $sheet->getColumnDimension($iValue['s_value'])->setWidth($iValue['s_width']);
//
//                // 设置第一行 行高
////            $sheet->getRowDimension(1)->setRowHeight(50);
//
////            $sheet->getStyle($iValue['s_cell'])->applyFromArray(self::$setArrayStyle);
//            }
//
//            //设置相对应值
//            foreach ($data as $n => $datum){
//
//                foreach ($header as $i => $iValue) {
//                    $letter = $this->getLetter($i);
//
//                    if (isset($datum[$iValue['s_cell']])){
//                        if (is_array ( $datum[$iValue['s_cell']])){
//                            $sheet->setCellValue ("{$char_index[$i]}".($n+2), $datum[$iValue['s_cell']]['text'] ); //setAttr
//                        }else{
//                            $sheet->setCellValue ("{$char_index[$i]}".($n+2), $datum[$iValue['s_cell']].' ');
//                        }
//                    }
//                }
//
//            }
//
//            $writer = new Xlsx($spreadsheet);
//
//            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//xlsx
//
//            header('Content-Disposition: attachment;filename='.'证书注册占比统计导出'.'.Xlsx');
//
//            header('Cache-Control: max-age=0');
//
//            $writer->save('php://output');
//
//            //删除清空：
//            $spreadsheet->disconnectWorksheets();
//
//            unset($spreadsheet);
//
////            $excel->outExcelStart($result, $header, '证书注册占比统计导出');
//            die;
//        }
//
//        return ReJson (message::$SUCC_CODE,message::$SUCC_SELECT_DATA,$result);
//    }

    public function getZszcProportionStatistics(
        Request          $request,
        Power            $powerService,
        System           $systemService,
        UserOrganization $organizationModel,
        User             $userModel,
        Excel $excel
    ){
        $param = $request->get ();
//        //获取当前登陆账号管理人员数据
        $uid = $request->param ('user_login_id');
        $idArr = $powerService->getUserDataRange ($uid);
        $type = $request->param ('type')?:1; //1全部 2公司证书 3倒证

        //如果有部门条件
        if ($param['org']){
            $OrgId = $systemService->getOrgIdStr($param['org'],$organizationModel->getStructure ());
            $idArr = $userModel->getOidUidDataModel ($OrgId);
        }
        //如果有人员条件
        if ($param['uid']){
            $idArr = [$param['uid']];
        }
        //默认查询当天数据
        $startDate = date('Y-m-d').' 00:00:00';
        $endDate = date('Y-m-d').' 23:59:59';
        if (!empty($param['start_date']) && !empty($param['end_date'])){
            $startDate = $param['start_date'].' 00:00:00';
            $endDate = $param['end_date'].' 23:59:59';
        }

        //查询默认条件分类
        //1.证书类别、等级  2.周期  3.B证(1带B ''不带B)  4.社保  返回给前段数据 （ 5 6 7 8）
        //  / 或条件  、与条件
        $whereDate = [
            [
                1=>'Ⅱ/建造师/一级', 2=>'3个月以内',3=>'',4=>'',5=>'一级建造师',6=>'裸证',7=>'三个月以内'
            ],
            [
                1=>'Ⅱ/建造师/一级', 2=>'3到6个月',3=>'',4=>'',5=>'一级建造师',6=>'裸证',7=>'三到六个月'
            ],
            [
                1=>'Ⅱ/建造师/一级', 2=>'6个月以上',3=>'',4=>'',5=>'一级建造师',6=>'裸证',7=>'六个月以上'
            ],
            [
                1=>'Ⅱ/建造师/一级', 2=>'3个月以内',3=>1,4=>'',5=>'一级建造师',6=>'带B证',7=>'三个月以内'
            ],
            [
                1=>'Ⅱ/建造师/一级', 2=>'3到6个月',3=>1,4=>'',5=>'一级建造师',6=>'带B证',7=>'三到六个月'
            ],
            [
                1=>'Ⅱ/建造师/一级', 2=>'6个月以上',3=>1,4=>'',5=>'一级建造师',6=>'带B证',7=>'六个月以上'
            ],
            [
                1=>'总计',2=>'',3=>'',4=>'',5=>'一级建造师',6=>'总计',7=>''
            ],


            [
                1=>'Ⅱ/建造师/二级', 2=>'3个月以内',3=>'',4=>'',5=>'二级建造师',6=>'裸证',7=>'三个月以内'
            ],
            [
                1=>'Ⅱ/建造师/二级', 2=>'3到6个月以上',3=>'',4=>'',5=>'二级建造师',6=>'裸证',7=>'三到六个月'
            ],
            [
                1=>'Ⅱ/建造师/二级', 2=>'6个月以上',3=>'',4=>'',5=>'二级建造师',6=>'裸证',7=>'六个月以上'
            ],
            [
                1=>'Ⅱ/建造师/二级', 2=>'3个月以内',3=>'1',4=>'',5=>'二级建造师',6=>'带B证',7=>'三个月以内'
            ],
            [
                1=>'Ⅱ/建造师/二级', 2=>'3到6个月',3=>'1',4=>'',5=>'二级建造师',6=>'带B证',7=>'三到六个月'
            ],
            [
                1=>'Ⅱ/建造师/二级', 2=>'6个月以上',3=>'1',4=>'',5=>'二级建造师',6=>'带B证',7=>'六个月以上'
            ],
            [
                1=>'总计',2=>'',3=>'',4=>'',5=>'二级建造师',6=>'总计',7=>''
            ],


            [
                1=>'Ⅱ/职称/初级', 2=>'3个月以内',4=>'',5=>'职称',6=>'初级',7=>'三个月以内'
            ],
            [
                1=>'Ⅱ/职称/初级', 2=>'3到6个月',4=>'',5=>'职称',6=>'初级',7=>'三到六个月'
            ],
            [
                1=>'Ⅱ/职称/初级', 2=>'6个月以上',4=>'',5=>'职称',6=>'初级',7=>'六个月以上'
            ],
            [
                1=>'Ⅱ/职称/中级', 2=>'3个月以内',4=>'',5=>'职称',6=>'中级',7=>'三个月以内'
            ],
            [
                1=>'Ⅱ/职称/中级', 2=>'3到6个月',4=>'',5=>'职称',6=>'中级',7=>'三到六个月'
            ],
            [
                1=>'Ⅱ/职称/中级', 2=>'6个月以上',4=>'',5=>'职称',6=>'中级',7=>'六个月以上'
            ],
            [
                1=>'Ⅱ/职称/高级', 2=>'3个月以内',4=>'',5=>'职称',6=>'高级',7=>'三个月以内'
            ],
            [
                1=>'Ⅱ/职称/高级', 2=>'3到6个月',4=>'',5=>'职称',6=>'高级',7=>'三到六个月'
            ],
            [
                1=>'Ⅱ/职称/高级', 2=>'6个月以上',4=>'',5=>'职称',6=>'高级',7=>'六个月以上'
            ],
            [
                1=>'总计',2=>'',4=>'',5=>'职称',6=>'总计',7=>''
            ],


            [
                1=>'Ⅱ/注册工程师', 2=>'3个月以内',4=>'',5=>'注册工程师',6=>'',7=>'三个月以内'
            ],
            [
                1=>'Ⅱ/注册工程师', 2=>'3到6个月',4=>'',5=>'注册工程师',6=>'',7=>'三到六个月'
            ],
            [
                1=>'Ⅱ/注册工程师', 2=>'6个月以上',4=>'',5=>'注册工程师',6=>'',7=>'六个月以上'
            ],
            [
                1=>'总计',2=>'',4=>'',5=>'Ⅱ/注册工程师',6=>'总计',7=>''
            ],


            [
                1=>'Ⅱ/八大员', 2=>'3个月以内',4=>'',5=>'八大员',6=>'',7=>'三个月以内'
            ],
            [
                1=>'Ⅱ/八大员', 2=>'3到6个月',4=>'',5=>'八大员',6=>'',7=>'三到六个月'
            ],
            [
                1=>'Ⅱ/八大员', 2=>'6个月以上',4=>'',5=>'八大员',6=>'',7=>'六个月以上'
            ],
            [
                1=>'总计',2=>'',4=>'',5=>'八大员',6=>'总计',7=>''
            ],

            [
                1=>'Ⅱ/三类人员', 2=>'3个月以内',4=>'',5=>'三类人员',6=>'',7=>'三个月以内'
            ],
            [
                1=>'Ⅱ/三类人员', 2=>'3到6个月',4=>'',5=>'三类人员',6=>'',7=>'三到六个月'
            ],
            [
                1=>'Ⅱ/三类人员', 2=>'6个月以上',4=>'',5=>'三类人员',6=>'',7=>'六个月以上'
            ],
            [
                1=>'总计',2=>'',4=>'',5=>'三类人员',6=>'总计',7=>''
            ]
        ];

        //查询公司证书
        $orderEnterPriseCompanyModel = new OrderEnterpriseCompany();
        $orderEnterPriseExchangeModel = new OrderEnterpriseExchange();
        $systemCertificateModel = new SystemCertificate();
        /**
         * 手写统计查询 条件过于混乱 不找规律了 固定模式
         */
        $wheres = 'certificate_leixing ='.$systemCertificateModel->getWhereCertificateIdModel('Ⅱ');
        $gszsTotal = $orderEnterPriseCompanyModel->getWhereAllModel ($wheres,$idArr,$startDate,$endDate); //公司证书查询
        $gzTotal =  $orderEnterPriseExchangeModel->getWhereAllModel($wheres,$idArr,$startDate,$endDate);//倒证查询
        //查询退单业绩
        $tdPrice = $this->getZszcChargeback($startDate,$endDate,$idArr,$this->erleiCertificate_id);
        $totalPerformance = ($gszsTotal['order_price'] + $gzTotal['order_price'] + $tdPrice)?:1;

        $result = [];

        $zj_no_order_num = 0;
        $zj_no_order_price = 0;
        $zj_order_num = 0;
        $zj_order_price = 0;

        if ($type == 1){
            foreach ($whereDate as $key => &$item) {
                $where = '';
                foreach ($item as $k => $itemTam) {

                    //查询是否是多条条件/  、
//                    if (!$itemTam) {
//                        continue;
//                    }

                    //是否计算总计
                    if ($itemTam === '总计'){

                        $result[$key]['leibie'] = $item[5];
                        $result[$key]['leixing'] = '总计';
                        $result[$key]['shijian'] = '';
                        $result[$key]['no_order_num'] = $zj_no_order_num;
                        $result[$key]['no_order_price'] = $zj_no_order_price;
                        $result[$key]['no_zhanbi'] = number_format ($zj_no_order_price/$totalPerformance,2)*100 .'%';

                        $result[$key]['order_num'] = $zj_order_num;
                        $result[$key]['order_price'] = $zj_order_price;
                        $result[$key]['zhanbi'] = number_format ($zj_order_price/$totalPerformance,2)*100 .'%';

                        if($totalPerformance == 1){
                            $result[$key]['total_performance'] = "";
                        }else{
                            $result[$key]['total_performance'] = $totalPerformance;
                        }


                        /**
                         * 初始化
                         */
                        $zj_no_order_num = 0;
                        $zj_no_order_price = 0;
                        $zj_order_num = 0;
                        $zj_order_price = 0;

                        continue 2;
                    }

                    //包含或条件 OR
                    if ($k == 1) {
                        $iArrData = explode ('/', $itemTam);

                        //查证书
                        if (count ($iArrData) == 3) {
                            $where = 'certificate_leixing = ' . $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0]);
                            $where .= ' and certificate_leibie = ' . ($systemCertificateModel->getWhereCertificateIdModel ($iArrData[1], $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0])) ?: 0);
                            $where .= ' and certificate_dengji = ' . ($systemCertificateModel->getWhereCertificateIdModel ($iArrData[2], $systemCertificateModel->getWhereCertificateIdModel ($iArrData[1])) ?: 0);
                        } else {
                            $where = 'certificate_leixing = ' . $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0]);
                            $where .= ' and certificate_leibie = ' . ($systemCertificateModel->getWhereCertificateIdModel ($iArrData[1], $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0])) ?: 0);
                        }

                    } elseif ($k == 2) {
                        //查周期
                        if ($itemTam == '3个月以内') {
                            $where .= ' and cycle <= ' . array_search ('3个月', stateData::$_cycle);
                        }else if($itemTam == "3到6个月"){
                            $where .= ' and cycle > ' . array_search ('3个月', stateData::$_cycle).' and cycle < '.array_search ('6个月', stateData::$_cycle);
                        } else if ($itemTam == '6个月以上') {
                            $where .= ' and cycle >= ' . array_search ('6个月', stateData::$_cycle);
                        }

                    } elseif ($k == 3) {
                        //查B证
                        if ($itemTam == 1){
                            $where .= ' and certificate_b > 0 and  certificate_b < 999';
                        }else{
                            $where .= ' and certificate_b = 999';
                        }

                    }
                }

                $whereNoSb = $where." and social_security = 999 ";//不交社保

                $gszsData = $orderEnterPriseCompanyModel->getWhereModel ($whereNoSb,$idArr,$startDate,$endDate); //公司证书不交社保
                $dzData = $orderEnterPriseExchangeModel->getWhereModel ($whereNoSb,$idArr,$startDate,$endDate); //倒证不交社保
                $result[$key]['leibie'] = $item[5];
                $result[$key]['leixing'] = $item[6];
                $result[$key]['shijian'] = $item[7];
                $result[$key]['no_order_num'] = $gszsData['order_num'] + $dzData['order_num'];
                $zj_no_order_num += $gszsData['order_num'] + $dzData['order_num'];//计总和

                $result[$key]['no_order_price'] = $gszsData['order_price'] + $dzData['order_price'];
                $zj_no_order_price += $gszsData['order_price'] + $dzData['order_price'];//计总和
                $result[$key]['no_zhanbi'] = number_format ( $result[$key]['no_order_price']/$totalPerformance,2)*100 .'%';

                $whereSb = $where." and social_security > 0 and social_security < 999  ";//交社保
                $gszsDatas = $orderEnterPriseCompanyModel->getWhereModel ($whereSb,$idArr,$startDate,$endDate); //公司证书交社保
                $dzDatas = $orderEnterPriseExchangeModel->getWhereModel ($whereSb,$idArr,$startDate,$endDate); //倒证交社保
                $result[$key]['order_num'] = $gszsDatas['order_num'] + $dzDatas['order_num'];
                $zj_order_num += $gszsDatas['order_num'] + $dzDatas['order_num'];//计总和
                $result[$key]['order_price'] = $gszsDatas['order_price'] + $dzDatas['order_price'];
                $zj_order_price += $gszsDatas['order_price'] + $dzDatas['order_price'];//计总和
                $result[$key]['zhanbi'] = number_format ($result[$key]['order_price']/$totalPerformance,2)*100 .'%';

                if($totalPerformance == 1){
                    $result[$key]['total_performance'] = "";
                }else{
                    $result[$key]['total_performance'] = $totalPerformance;
                }
            }
        }else if ($type == 2){

            foreach ($whereDate as $key => &$item) {
                $where = '';
                foreach ($item as $k => $itemTam) {

                    //查询是否是多条条件/  、
//                    if (!$itemTam) {
//                        continue;
//                    }

                    //是否计算总计
                    if ($itemTam === '总计'){

                        $result[$key]['leibie'] = $item[5];
                        $result[$key]['leixing'] = '总计';
                        $result[$key]['shijian'] = '';
                        $result[$key]['no_order_num'] = $zj_no_order_num;
                        $result[$key]['no_order_price'] = $zj_no_order_price;
                        $result[$key]['no_zhanbi'] = number_format ($zj_no_order_price/$totalPerformance,2)*100 .'%';

                        $result[$key]['order_num'] = $zj_order_num;
                        $result[$key]['order_price'] = $zj_order_price;
                        $result[$key]['zhanbi'] = number_format ($zj_order_price/$totalPerformance,2)*100 .'%';

                        if($totalPerformance == 1){
                            $result[$key]['total_performance'] = "";
                        }else{
                            $result[$key]['total_performance'] = $totalPerformance;
                        }

                        /**
                         * 初始化
                         */
                        $zj_no_order_num = 0;
                        $zj_no_order_price = 0;
                        $zj_order_num = 0;
                        $zj_order_price = 0;

                        continue 2;
                    }

                    //包含或条件 OR
                    if ($k == 1) {
                        $iArrData = explode ('/', $itemTam);

                        //查证书
                        if (count ($iArrData) == 3) {
                            $where = 'certificate_leixing = ' . $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0]);
                            $where .= ' and certificate_leibie = ' . ($systemCertificateModel->getWhereCertificateIdModel ($iArrData[1], $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0])) ?: 0);
                            $where .= ' and certificate_dengji = ' . ($systemCertificateModel->getWhereCertificateIdModel ($iArrData[2], $systemCertificateModel->getWhereCertificateIdModel ($iArrData[1])) ?: 0);
                        } else {
                            $where = 'certificate_leixing = ' . $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0]);
                            $where .= ' and certificate_leibie = ' . ($systemCertificateModel->getWhereCertificateIdModel ($iArrData[1], $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0])) ?: 0);
                        }

                    } elseif ($k == 2) {
                        //查周期
                        if ($itemTam === '3个月以内') {
                            $where .= ' and cycle <= ' . array_search ('3个月', stateData::$_cycle);
                        }else if($itemTam === "3到6个月以上"){
                            $where .= ' and cycle > ' . array_search ('3个月', stateData::$_cycle).' and cycle < '.array_search ('6个月', stateData::$_cycle);
                        } else if ($itemTam === '6个月以上') {
                            $where .= ' and cycle >= ' . array_search ('6个月', stateData::$_cycle);
                        }

                    } elseif ($k == 3) {
                        //查B证
                        if ($itemTam == 1){
                            $where .= ' and certificate_b > 0 and  certificate_b < 999';
                        }else{
                            $where .= ' and certificate_b = 999';
                        }

                    }
//                    else {
//                        //查社保
//                        if (!$itemTam){
//                            $where .= " and (social_security = 0 OR social_security = '')";
//                        }else{
//                            $where .= ' and social_security > 0 ';
//                        }
//
//                    }
                }

                $whereNoSb = $where." and social_security = 999 ";//不交社保

                $gszsData = $orderEnterPriseCompanyModel->getWhereModel ($whereNoSb,$idArr,$startDate,$endDate); //公司证书不交社保
                $result[$key]['leibie'] = $item[5];
                $result[$key]['leixing'] = $item[6];
                $result[$key]['shijian'] = $item[7];
                $result[$key]['no_order_num'] = $gszsData['order_num'];
                $zj_no_order_num += $gszsData['order_num'];//计总和

                $result[$key]['no_order_price'] = $gszsData['order_price'];
                $zj_no_order_price += $gszsData['order_price'];//计总和
                $result[$key]['no_zhanbi'] = number_format ($gszsData['order_price']/$totalPerformance,2)*100 .'%';

                $whereSb = $where." and social_security > 0 and social_security < 999  ";//交社保
                $gszsDatas = $orderEnterPriseCompanyModel->getWhereModel ($whereSb,$idArr,$startDate,$endDate); //公司证书交社保
                $result[$key]['order_num'] = $gszsDatas['order_num'];
                $zj_order_num += $gszsDatas['order_num'];//计总和
                $result[$key]['order_price'] = $gszsDatas['order_price'];
                $zj_order_price += $gszsDatas['order_price'];//计总和
                $result[$key]['zhanbi'] = number_format ($gszsDatas['order_price']/$totalPerformance,2)*100 .'%';

                if($totalPerformance == 1){
                    $result[$key]['total_performance'] = "";
                }else{
                    $result[$key]['total_performance'] = $totalPerformance;
                }
            }

        }else if($type == 3){

            foreach ($whereDate as $key => &$item) {

                $where = '';
                foreach ($item as $k => $itemTam) {

                    //查询是否是多条条件/  、
//                    if (!$itemTam) {
//                        continue;
//                    }

                    //是否计算总计
                    if ($itemTam === '总计'){

                        $result[$key]['leibie'] = $item[5];
                        $result[$key]['leixing'] = '总计';
                        $result[$key]['shijian'] = '';
                        $result[$key]['no_order_num'] = $zj_no_order_num;
                        $result[$key]['no_order_price'] = $zj_no_order_price;
                        $result[$key]['no_zhanbi'] = number_format ($zj_no_order_price/$totalPerformance,2)*100 .'%';

                        $result[$key]['order_num'] = $zj_order_num;
                        $result[$key]['order_price'] = $zj_order_price;
                        $result[$key]['zhanbi'] = number_format ($zj_order_price/$totalPerformance,2)*100 .'%';

                        if($totalPerformance == 1){
                            $result[$key]['total_performance'] = "";
                        }else{
                            $result[$key]['total_performance'] = $totalPerformance;
                        }

                        /**
                         * 初始化
                         */
                        $zj_no_order_num = 0;
                        $zj_no_order_price = 0;
                        $zj_order_num = 0;
                        $zj_order_price = 0;

                        continue 2;
                    }

                    //包含或条件 OR
                    if ($k == 1) {
                        $iArrData = explode ('/', $itemTam);

                        //查证书
                        if (count ($iArrData) == 3) {
                            $where = 'certificate_leixing = ' . $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0]);
                            $where .= ' and certificate_leibie = ' . ($systemCertificateModel->getWhereCertificateIdModel ($iArrData[1], $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0])) ?: 0);
                            $where .= ' and certificate_dengji = ' . ($systemCertificateModel->getWhereCertificateIdModel ($iArrData[2], $systemCertificateModel->getWhereCertificateIdModel ($iArrData[1])) ?: 0);
                        } else {
                            $where = 'certificate_leixing = ' . $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0]);
                            $where .= ' and certificate_leibie = ' . ($systemCertificateModel->getWhereCertificateIdModel ($iArrData[1], $systemCertificateModel->getWhereCertificateIdModel ($iArrData[0])) ?: 0);
                        }

                    } elseif ($k == 2) {
                        //查周期
                        if ($itemTam === '3个月以内') {
                            $where .= ' and cycle <= ' . array_search ('3个月', stateData::$_cycle);
                        }else if($itemTam === "3到6个月以上"){
                            $where .= ' and cycle > ' . array_search ('3个月', stateData::$_cycle).' and cycle < '.array_search ('6个月', stateData::$_cycle);
                        } else if ($itemTam === '6个月以上') {
                            $where .= ' and cycle >= ' . array_search ('6个月', stateData::$_cycle);
                        }

                    } elseif ($k == 3) {

                        //查B证
                        if ($itemTam == 1){
                            $where .= ' and certificate_b > 0 and  certificate_b < 999';
                        }else{
                            $where .= ' and certificate_b = 999';
                        }

                    }
                }
//                print_r ($where);
                $whereNoSb = $where." and social_security = 999 ";//不交社保

//            echo PHP_EOL;
//            echo "-----------";
                $gszsData = $orderEnterPriseExchangeModel->getWhereModel ($whereNoSb,$idArr,$startDate,$endDate); //倒证不交社保
                $result[$key]['leibie'] = $item[5];
                $result[$key]['leixing'] = $item[6];
                $result[$key]['shijian'] = $item[7];
                $result[$key]['no_order_num'] = $gszsData['order_num'];
                $zj_no_order_num += $gszsData['order_num'];//计总和

                $result[$key]['no_order_price'] = $gszsData['order_price'];
                $zj_no_order_price += $gszsData['order_price'];//计总和
                $result[$key]['no_zhanbi'] = number_format ($gszsData['order_price']/$totalPerformance,2)*100 .'%';

                $whereSb = $where." and social_security > 0 and social_security < 999  ";//交社保
                $gszsDatas = $orderEnterPriseExchangeModel->getWhereModel ($whereSb,$idArr,$startDate,$endDate); //倒证交社保
                $result[$key]['order_num'] = $gszsDatas['order_num'];
                $zj_order_num += $gszsDatas['order_num'];//计总和
                $result[$key]['order_price'] = $gszsDatas['order_price'];
                $zj_order_price += $gszsDatas['order_price'];//计总和
                $result[$key]['zhanbi'] = number_format ($gszsDatas['order_price']/$totalPerformance,2)*100 .'%';

                if($totalPerformance == 1){
                    $result[$key]['total_performance'] = "";
                }else{
                    $result[$key]['total_performance'] = $totalPerformance;
                }
            }
        }


        //导出
        if ($param['exports']) {
            $headName = '证书注册类别占比';
//            if ($type == 1){
//                $headName = '公司证书';
//            }else if(){
//                $headName = '外部证书';
//            }
//            foreach ($result as $k => &$resultDatum) {
//                if ($k >= 10) {
//                    $resultDatum['type'] = '外部证书';
//                    $k -= 10;
//                } else {
//                    $resultDatum['type'] = '公司证书';
//                }
//            }


            $header = [
//                ['s_cell' => 'id', 's_value' => '序号', 's_width' => 10],
                ['s_cell' => 'leibie', 's_value' => '类别', 's_width' => 10],
                ['s_cell' => 'leixing', 's_value' => '类型', 's_width' => 10],
                ['s_cell' => 'shijian', 's_value' => '时间', 's_width' => 10],
                ['s_cell' => 'no_order_num', 's_value' => '(不交)单量', 's_width' => 10],
                ['s_cell' => 'no_order_price', 's_value' => '(不交)业绩', 's_width' => 10],
                ['s_cell' => 'no_zhanbi', 's_value' => '(不交)业绩占比', 's_width' => 10],
                ['s_cell' => 'order_num', 's_value' => '(交社保)单量', 's_width' => 10],
                ['s_cell' => 'order_price', 's_value' => '(交社保)业绩', 's_width' => 10],
                ['s_cell' => 'zhanbi', 's_value' => '(交社保)业绩占比', 's_width' => 10],
                ['s_cell' => 'total_performance', 's_value' => '总业绩', 's_width' => 10],
            ];

            $data = array_values ($result);
            $spreadsheet = new Spreadsheet();

            $spreadsheet->getActiveSheet ()->mergeCells ("A2:A8");
            $spreadsheet->getActiveSheet ()->mergeCells ("B2:B4");
            $spreadsheet->getActiveSheet ()->mergeCells ("B5:B7");

            $spreadsheet->getActiveSheet ()->mergeCells ("A9:A15");
            $spreadsheet->getActiveSheet ()->mergeCells ("B9:B11");
            $spreadsheet->getActiveSheet ()->mergeCells ("B12:B14");

            $spreadsheet->getActiveSheet ()->mergeCells ("A16:A25");
            $spreadsheet->getActiveSheet ()->mergeCells ("B16:B18");
            $spreadsheet->getActiveSheet ()->mergeCells ("B19:B21");
            $spreadsheet->getActiveSheet ()->mergeCells ("B22:B24");

            $spreadsheet->getActiveSheet ()->mergeCells ("A26:A29");
            $spreadsheet->getActiveSheet ()->mergeCells ("B26:B28");

            $spreadsheet->getActiveSheet ()->mergeCells ("A30:A33");
            $spreadsheet->getActiveSheet ()->mergeCells ("B30:B32");

            $spreadsheet->getActiveSheet ()->mergeCells ("A34:A37");
            $spreadsheet->getActiveSheet ()->mergeCells ("B34:B36");


            $sheet = $spreadsheet->getActiveSheet ();
            $sheet->getStyle ('A2:A8')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('B2:B4')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('B5:B7')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('A9:A15')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('B9:B11')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('B12:B14')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('A16:A25')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('B16:B18')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('B19:B21')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('B22:B24')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('A26:A29')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('A30:A33')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            $sheet->getStyle ('A34:A37')->getAlignment ()->setVertical (Alignment::VERTICAL_CENTER);
            //设置excel格式
            $char_index = [];
            for ($x = 'A'; $x < 'ZZ'; $x++) {
                array_push ($char_index, $x);
            }
            $letter = '';
            //设置表头
            foreach ($header as $i => $iValue) {
                //设置字母
                $letter = $this->getLetter ($i);
                $sheet->setCellValue ("{$char_index[$i]}1", $iValue['s_value']);

                // 设置 A 列 列宽
//            $sheet->getColumnDimension($iValue['s_value'])->setWidth($iValue['s_width']);

                // 设置第一行 行高
//            $sheet->getRowDimension(1)->setRowHeight(50);

//            $sheet->getStyle($iValue['s_cell'])->applyFromArray(self::$setArrayStyle);
            }

            //设置相对应值
            foreach ($data as $n => $datum) {

                foreach ($header as $i => $iValue) {
                    $letter = $this->getLetter ($i);

                    if (isset($datum[$iValue['s_cell']])) {
                        if (is_array ($datum[$iValue['s_cell']])) {
                            $sheet->setCellValue ("{$char_index[$i]}" . ($n + 2), $datum[$iValue['s_cell']]['text']); //setAttr
                        } else {
                            $sheet->setCellValue ("{$char_index[$i]}" . ($n + 2), $datum[$iValue['s_cell']] . ' ');
                        }
                    }
                }

            }

            $writer = new Xlsx($spreadsheet);

            header ('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//xlsx

            header ('Content-Disposition: attachment;filename=' . $headName . '.Xlsx');

            header ('Cache-Control: max-age=0');

            $writer->save ('php://output');

            //删除清空：
            $spreadsheet->disconnectWorksheets ();

            unset($spreadsheet);

//            $excel->outExcelStart($result, $header, '证书注册占比统计导出');
            die;
        }

        return ReJson (message::$SUCC_CODE,message::$SUCC_SELECT_DATA,$result);
    }




    /**
     * 报表中心-提成比列
     */
    public function getCommission(
        Request                       $request,
        SystemBusinessCommissionRatio $businessCommissionRatioModel,
        SystemBusiness                $businessModel,
        User                          $userModel,
        Power $powerService,
        System $systemService,
        UserOrganization $organizationModel,
        OrderOtherCertificateBusiness $otherCertificateBusinessModel,
        OrderEnterpriseCertificateBusiness $orderEnterpriseCertificateBusinessModel,
        OrderEnterpriseCertificateRegister $orderEnterpriseCertificateRegisterModel,
        OrderEnterpriseExchange $orderEnterpriseExchangeModel,
        SystemBusinessCommissionRatio $systemBusinessCommissionRatioModel,
        Excel $excel
    ){
        $param = $request->get ();
        //获取当前登陆账号管理人员数据
        $uid = $request->param ('user_login_id');
        $idArr = $powerService->getUserDataRange ($uid);

        if (!$param['page'] || !$param['pageNum']){
            return ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);
        }

        //如果有部门条件
        if (!empty($param['org'])){
            $OrgId = $systemService->getOrgIdStr($param['org'],$organizationModel->getStructure ());
            $idArr = $userModel->getOidUidDataModel ($OrgId);
        }

        //如果有人员条件
        if (!empty($param['uid'])){
            $idArr = $param['uid'];
        }

        //默认查询当月数据
        $startDate = date('Y-m-01').' 00:00:00';
        $endDate = date('Y-m-t').' 23:59:59';
        if (!empty($param['month'])){
            $startDate = date('Y-m-01',strtotime ($param['month'])).' 00:00:00';
            $endDate = date('Y-m-t',strtotime ($param['month'])).' 23:59:59';
        }

        //获取所有的业务
        $businessData = $businessModel->getSubTypeArrDataModel ();

        $userData = $userModel->getIdUserFieldRandInfo($idArr, $param['page'],$param['pageNum'],$startDate,$endDate);
        $allData = $userModel->getIdUserCountInfo ($idArr,$startDate,$endDate);
        $result = [];
        //填充数据
        foreach ($userData as $item) {
            foreach ($businessData as  $businessDatum) {

                $rdata = [];
                $rdata['performance'] = 0;
                //填充用户信息
                $rdata['nickname'] = $item['nickname'];
                $rdata['org'] = $item['department_id'];
//                $rdata['ratio'] = ''; //抽成比例
//                $rdata['ratio_price'] = ''; //提成

                if ($businessDatum['business_category']){
                    $rdata['business_name'] = $businessDatum['business_category'];
                }else{
                    $rdata['business_name'] = $businessDatum['business_name'];
                }

                /**
                 * 查询证书办理对应业务业绩
                 */
                $rdata['performance'] += $otherCertificateBusinessModel->getTcblModel ($businessDatum['id'],$item['id'],$startDate,$endDate);

                /**
                 * 查询企业证书对应业务业绩
                 */

                $rdata['performance'] += $orderEnterpriseCertificateBusinessModel->getTcblModel ($businessDatum['id'],$item['id'],$startDate,$endDate);

                /**
                 * 查询证书注册对应业务业绩
                 */
                if(in_array ($rdata['business_name'],$this->business_gongsizhengshu)){
                    $rdata['performance'] += $orderEnterpriseCertificateRegisterModel->getTcblModel($businessDatum['id'],$item['id'],$startDate,$endDate);
                }

                /**
                 * 查询倒证对应业务业绩
                 */
                if ($rdata['business_name'] == $this->business_daozheng){
                    $rdata['performance'] += $orderEnterpriseCertificateRegisterModel->getDzTcblModel($businessDatum['id'],$item['id'],$startDate,$endDate);
                }

                //获取业务提成比例
                $ratioValue = $systemBusinessCommissionRatioModel->getBidBetweenValueModel($businessDatum['id'],$rdata['performance'],$rdata['performance']);

                $rdata['ratio'] = $ratioValue.'%';
                $rdata['ratio_price'] = $rdata['performance'] * $ratioValue / 100;

                $result[] = $rdata;
            }
        }

        if($param['exports']){
            $header = [
//                ['s_cell' => 'id', 's_value' => '序号', 's_width' => 10],
                ['s_cell' => 'org', 's_value' => '公司部门', 's_width' => 10],
                ['s_cell' => 'nickname', 's_value' => '人员姓名', 's_width' => 10],
                ['s_cell' => 'business_name', 's_value' => '业务类型', 's_width' => 10],
                ['s_cell' => 'performance', 's_value' => '业绩总数', 's_width' => 10],
                ['s_cell' => 'ratio', 's_value' => '抽成比例', 's_width' => 10],
                ['s_cell' => 'ratio_price', 's_value' => '提成', 's_width' => 10]
            ];

            //是否是选择性的导出
            if($param['all'] != 1){
                if ($param['index']){
                    $index = explode (',',$param['index']);
                    $exData = [];
                    foreach ($index as $indexs) {
                        $exData[] = $result[$indexs-1];
                    }
                    $result = $exData;
                }
            }

            $excel->outExcelStart($result, $header, '提成比例导出');
            die;
        }

        return ReJson (
            message::$SUCC_CODE,
            message::$SUCC_SELECT_DATA,
            $result,
            ['total'=>$allData, 'pageNum'=>$param['pageNum']]
        );
    }


    /**
     * 报表中心-流水
     */
    public function getFlowingWater(
        Request                $request,
        OrderTransactionRecord $orderTransactionRecordModel,
        Power $powerService,
        System $systemService,
        UserOrganization $organizationModel,
        User $userModel,
        OrderOtherCertificateBusiness $otherCertificateBusinessModel,
        OrderEnterpriseCertificateBusiness $orderEnterpriseCertificateBusinessModel,
        OrderEnterpriseCompany $companyModel,
        OrderPersonCertificateRegister $personCertificateRegisterModel,
        OrderEnterpriseExchange $exchangeModel,
        OrderTransactionLog $orderTransactionLogModel,
        Excel $excel,
        FinanceServer $financeServer,
        RedisService $redisService
    ){
        $param = $request->get ();
        //获取当前登陆账号管理人员数据
        $uid = $request->get ('user_login_id');
        $type = $request->get ('type')?:1; //1收款 2付款
        $idArr = $powerService->getUserDataRange ($uid);
        $all = $request->get ('all');//是否导出全部数据

        if (!$param['page'] && !$param['pageNum']){
            return ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);
        }
        //如果有部门条件
        if ($param['org']){
            $OrgId = $systemService->getOrgIdStr($param['org'],$organizationModel->getStructure ());
            $idArr = $userModel->getOidUidDataModel ($OrgId);
        }

        //如果有人员条件
        if ($param['uid']){
            $idArr = $param['uid'];
        }
        //如果有审批人条件
        if ($param['sid']){
            $shrIdArr = $param['sid'];
            $idArr = '';
        }else{
            $shrIdArr = '';
        }

        //默认查询当天数据
        $startDate = date('Y-m-d').' 00:00:00';
        $endDate = date('Y-m-d').' 23:59:59';
        if (!empty($param['start_date']) && !empty($param['end_date'])){
            $startDate = $param['start_date'].' 00:00:00';
            $endDate = $param['end_date'].' 23:59:59';
        }

        //查询收付款基本信息
        if (!empty($all)){
            $transId = $orderTransactionRecordModel->getTypeStatusAllDataModel ($type,$startDate,$endDate,$idArr,$shrIdArr);
        }else{
            $transId = explode (',',$param['id']);
        }

        $data = $redisService->getFlowingList ($transId);
        /**
         * 计算总计 审批时间筛选
         */
        $allPrice = 0;
        $reData = [];
//        print_r ($startDate);
//        echo PHP_EOL;
//        print_r ($endDate);die;
        foreach ($data as $datum) {
            //筛选审批时间
            if (!isset($datum['examine_date'])){
                continue;
            }
            if (strtotime ($datum['examine_date']) >= strtotime ($startDate) && strtotime ($datum['examine_date']) <= strtotime ($endDate)){
                $reData[] = $datum;
                $allPrice += $datum['price'];
            }
        }
//        if ($transId){
//            //拼接订单数据
//            foreach ($transId as &$datum) {
//                $result = $otherCertificateBusinessModel->getCerDataPersonModel($datum['certificate_code'],'person_name');
//                if (!empty($result)){
//                    $datum['person_name'] = $result;
//                    $datum['enterprise_name'] = '';
//                    $datum['employ_unit_title'] = '';
//                    $datum['reg_unit'] = '';
//
//                    //审批时间
//                    $datum['examine_date'] = $orderTransactionLogModel->getExamineDateModel($datum['id']) ?: '';
//
//                    //筛选审批时间
//                    if (strtotime ($datum['examine_date']) >= strtotime ($startDate) && strtotime ($datum['examine_date']) <= strtotime ($endDate)){
//                        //审批人部门
//                        $datum['examine_org'] = $systemService->getOrgStrName ($userModel->getUidOrgId ($datum['examine_uid']['id']));
//                        $reData[] = $datum;
//                        $allPrice += $datum['price'];
//                    }
//
//                    continue;
//                }
//
//                $result = $orderEnterpriseCertificateBusinessModel->getCerFieldDataModel($datum['certificate_code']);
//                if (!empty($result)){
//                    $datum['person_name'] = '';
//                    if (isset($result['enterprise_name'])){
//                        $datum['enterprise_name'] = $result['enterprise_name'];
//                    }else{
//                        $datum['enterprise_name'] = '';
//                    }
//                    $datum['employ_unit_title'] = '';
//                    $datum['reg_unit'] = '';
//
//                    //审批时间
//                    $datum['examine_date'] = $orderTransactionLogModel->getExamineDateModel($datum['id']) ?: '';
//
//                    //筛选审批时间
//                    if (strtotime ($datum['examine_date']) >= strtotime ($startDate) && strtotime ($datum['examine_date']) <= strtotime ($endDate)){
//                        //审批人部门
//                        $datum['examine_org'] = $systemService->getOrgStrName ($userModel->getUidOrgId ($datum['examine_uid']['id']));
//                        $reData[] = $datum;
//                        $allPrice += $datum['price'];
//                    }
//
//                    continue;
//                }
//
//                $result = $companyModel->getFinanceFieldFindModel($datum['certificate_code']);
//                if (!empty($result)){
//                    $datum['person_name'] = $result['person_name'];
//                    $datum['enterprise_name'] = '';
//                    $datum['employ_unit_title'] = $result['employ_unit_title'];
//                    $datum['reg_unit'] = $result['reg_unit'];
//
//                    //审批时间
//                    $datum['examine_date'] = $orderTransactionLogModel->getExamineDateModel($datum['id']) ?: '';
//
//                    //筛选审批时间
//                    if (strtotime ($datum['examine_date']) >= strtotime ($startDate) && strtotime ($datum['examine_date']) <= strtotime ($endDate)){
//                        //审批人部门
//                        $datum['examine_org'] = $systemService->getOrgStrName ($userModel->getUidOrgId ($datum['examine_uid']['id']));
//                        $reData[] = $datum;
//                        $allPrice += $datum['price'];
//                    }
//                    continue;
//                }
//
//                $result = $exchangeModel->getFinanceFieldFindModel($datum['certificate_code']);
//                if (!empty($result)){
//                    $datum['person_name'] = $result['person_name'];
//                    $datum['enterprise_name'] = isset($result['enterprise_name'])?:'';
//                    $datum['employ_unit_title'] = $result['employ_unit_title'];
//                    $datum['reg_unit'] = $result['unit_reg_title'];
//
//                    //审批时间
//                    $datum['examine_date'] = $orderTransactionLogModel->getExamineDateModel($datum['id']) ?: '';
//                    //筛选审批时间
//                    if (strtotime ($datum['examine_date']) >= strtotime ($startDate) && strtotime ($datum['examine_date']) <= strtotime ($endDate)){
//                        //审批人部门
//                        $datum['examine_org'] = $systemService->getOrgStrName ($userModel->getUidOrgId ($datum['examine_uid']['id']));
//                        $reData[] = $datum;
//                        $allPrice += $datum['price'];
//                    }
//                    continue;
//                }
//
//                if (!isset($datum['employ_unit_title'])){
//                    $datum['employ_unit_title'] = '';
//                    $datum['reg_unit'] = '';
//                    $datum['person_name'] = '';
//                    $datum['enterprise_name'] = '';
//                }
//            }
//        }

        //合计
        $dataa['id'] = '合计';
        $dataa['price'] = $allPrice;

        //添加合计
        $result = array_slice(my_sort($reData, 'examine_date', SORT_DESC), ($param['page'] - 1) * $param['pageNum'], $param['pageNum']);

        $count = count($result);

        if ($count >= $param['pageNum']){
            $result[$param['pageNum']] = $dataa;
        }else{
            $result[$count] = $dataa;
        }
        //导出
        if (isset($param['exports']) && $param['exports'] == 1){
            if ($type == 1){
                $header = [
                    ['s_cell' => 'id', 's_value' => '序号', 's_width' => 10],
                    ['s_cell' => 'transaction_code', 's_value' => '收款单号', 's_width' => 10],
                    ['s_cell' => 'certificate_code', 's_value' => '关联订单编号', 's_width' => 10],
                    ['s_cell' => 'employ_unit_title', 's_value' => '聘用单位名称', 's_width' => 10],
                    ['s_cell' => 'person_name', 's_value' => '人才姓名', 's_width' => 10],
                    ['s_cell' => 'settlement_method', 's_value' => '结算方式', 's_width' => 10],
                    ['s_cell' => 'type', 's_value' => '收款类型', 's_width' => 10],
                    ['s_cell' => 'price', 's_value' => '收款金额', 's_width' => 10],
                    ['s_cell' => 'submit_date', 's_value' => '到账时间', 's_width' => 10],
                    ['s_cell' => 'transaction_org', 's_value' => '收款人公司部门', 's_width' => 10],
                    ['s_cell' => 'transaction_uid', 's_value' => '收款人员', 's_width' => 10],
                    ['s_cell' => 'examine_date', 's_value' => '审批时间', 's_width' => 10],
                    ['s_cell' => 'examine_org', 's_value' => '审批人公司部门', 's_width' => 10],
                    ['s_cell' => 'examine_uid', 's_value' => '审批人员', 's_width' => 10]
                ];
            }else{
                $header = [
                    ['s_cell' => 'id', 's_value' => '序号', 's_width' => 10],
                    ['s_cell' => 'transaction_code', 's_value' => '付款单号', 's_width' => 10],
                    ['s_cell' => 'certificate_code', 's_value' => '关联订单编号', 's_width' => 10],
                    ['s_cell' => 'employ_unit_title', 's_value' => '聘用单位名称', 's_width' => 10],
                    ['s_cell' => 'person_name', 's_value' => '人才姓名', 's_width' => 10],
                    ['s_cell' => 'settlement_method', 's_value' => '结算方式', 's_width' => 10],
                    ['s_cell' => 'type', 's_value' => '付款类型', 's_width' => 10],
                    ['s_cell' => 'price', 's_value' => '付款金额', 's_width' => 10],
                    ['s_cell' => 'submit_date', 's_value' => '到账时间', 's_width' => 10],
                    ['s_cell' => 'transaction_org', 's_value' => '付款人公司部门', 's_width' => 10],
                    ['s_cell' => 'transaction_uid', 's_value' => '付款人员', 's_width' => 10],
                    ['s_cell' => 'examine_date', 's_value' => '审批时间', 's_width' => 10],
                    ['s_cell' => 'examine_org', 's_value' => '审批人公司部门', 's_width' => 10],
                    ['s_cell' => 'examine_uid', 's_value' => '审批人员', 's_width' => 10]
                ];
            }


            $excel->outExcelStart($result, $header, '流水导出');
            die;
        }

        return ReJson (
            message::$SUCC_CODE,
            message::$SUCC_SELECT_DATA,
            $result,
            [
                'total'=>count ($reData),
                'pageNum'=>$param['pageNum']
            ]
        );
    }



    /**
     * 获取总业绩
     * @param $moonStart
     * @param $moonEnd
     * @param $uidArr
     * @param string $cerType
     * @return float|int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTotal($moonStart, $moonEnd, $uidArr, $cerType='')
    {

        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $other = new OrderOtherCertificateBusiness();
        $business = new OrderEnterpriseCertificateBusiness();
        $orderDealChargebackModel = new OrderDealChargeback();
        $certificateModel = new SystemCertificate();

        //企业证书注册
        $enterData = $company->getSummary($moonStart,$moonEnd,$uidArr,$cerType);

        //企业倒证
        $exchangeData = $exchange->getSummaryModel($moonStart,$moonEnd,$uidArr,$cerType);

        //证书办理
        $otherData = $other->getSummaryModel($moonStart,$moonEnd,$uidArr,$cerType);

        //企业证书
        $businessData = $business->getSummaryModel($moonStart,$moonEnd,$uidArr);

        //查询时间段退单业绩
        $tdPrice = $this->getChargeback($moonStart,$moonEnd,$uidArr,$cerType);
//        $tdPrice = 0;

        //总业绩
        if($cerType && $cerType != $this->erleiCertificate_id){
            $total = $exchangeData['performance_sum']+$otherData['performance_sum']+$enterData; //如果有证书 不统计企业证书业绩
        }else{
            $total = $exchangeData['performance_sum']+$otherData['performance_sum']+$businessData+$enterData;
        }

        //总业绩-退单金额
        return $total+$tdPrice;
    }

    //获取公司证书业绩
    /**
     * @param $startDate
     * @param $endDate
     * @param $idArr
     * @param $type //1查询公司证书 2查询倒证 3查询证书办理 4查询企业证书
     * @return float|int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGsYj($startDate,$endDate,$idArr,$type)
    {
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $other = new OrderOtherCertificateBusiness();
        $business = new OrderEnterpriseCertificateBusiness();
        $orderDealChargebackModel = new OrderDealChargeback();
        $total= 0;
        //企业证书注册
        if ($type == 1){
            $enterData = $company->getSummary($startDate,$endDate,$idArr);

            //查询时间段退单业绩
            $tdPrice = $this->getTypeChargeback($startDate,$endDate,$idArr,1);

            //证书注册总业绩需要拿单位价/人数-人才证书的收证价格
//            foreach ($enterData as $enterDatum) {
//                $total += $enterDatum['performance_all'];
//            }

            $total += $tdPrice + $enterData;

        }
        if ($type == 2){
            $exchangeData = $exchange->getSummaryModel($startDate,$endDate,$idArr);

            //查询时间段退单业绩
            $tdPrice = $this->getTypeChargeback($startDate,$endDate,$idArr,2);
            $total += $exchangeData['performance_sum']+$tdPrice;
        }
        if ($type == 3){
            //证书办理
            $otherData = $other->getSummaryModel($startDate,$endDate,$idArr);

            //查询时间段退单业绩
            $tdPrice = $this->getTypeChargeback($startDate,$endDate,$idArr,3);

            $total += $otherData['performance_sum']+$tdPrice;

        }

        if ($type == 4){
            //企业证书
            $businessData = $business->getSummaryModel($startDate,$endDate,$idArr);

            //查询时间段退单业绩
            $tdPrice = $this->getTypeChargeback($startDate,$endDate,$idArr,4);
            $total += $businessData+$tdPrice;
        }


        //总业绩-退单金额
        return $total;
    }

    /**
     * 查询退单金额
     */
    public function getChargeback($meekStart,$meekEnd,$uidArr,$cerId='')
    {
        //查询退单金额
        //查询时间段退单业绩
        $orderDealChargebackModel = new OrderDealChargeback();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $other = new OrderOtherCertificateBusiness();
        $business = new OrderEnterpriseCertificateBusiness();
        $systemCertificate = new SystemCertificate();
        $certificateModel = new SystemCertificate();

        $tdData = $orderDealChargebackModel->getBetDateTypeModel($meekStart,$meekEnd);
        $tdPrice = 0;
        if ($tdData){
            foreach ($tdData as $item) {
                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $comDa = $company->getIdCertificateCodeFindModel($item['order_company_id'],$item['certificate_code']);
                if ($comDa){
//                    print_r ($comDa);die;
                    //如果有证书 先判断证书是否相等
//                    $cid = $certificateModel->getWhereCertificateIdModel($comDa['certificate_leixing']);
                    $cid = $comDa['certificate_id'][0]; //证书类型
                    if ($cerId){
                        if ($cid == $cerId){
                            if(is_array ($uidArr)){
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $comDa['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr, true)) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            }else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $comDa['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            }
                        }else{
                            continue;
                        }
                    }else{
                        if(is_array ($uidArr)){
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $comDa['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if (in_array ($performanceArrItem, $uidArr, true)) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        }else {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $comDa['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if ($performanceArrItem == $uidArr) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        }
                    }
                }

                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $exDa = $exchange->getWhereIdDataSelectModel($item['order_company_id'],$item['certificate_code']);
                if ($exDa) {
                    $cid = $exDa[0]['certificate_leixing']['id'];
                    if ($cerId){
                        if ($cid == $cerId) {
                            if(is_array ($uidArr)){
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $exDa[0]['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr)) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            }else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $exDa[0]['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            }
                        }else{
                            continue;
                        }
                    }else{
                        if(is_array ($uidArr)){
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $exDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if (in_array ($performanceArrItem, $uidArr)) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        }else {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $exDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if ($performanceArrItem == $uidArr) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        }
                    }
                }

                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $otDa = $other->getWhereIdDataSelectModel($item['order_company_id'],$item['certificate_code']);
                if ($otDa) {
                    $cid = $otDa[0]['certificate_leixing']['id'];
                    if ($cerId){
                        if ($cid == $cerId) {
                            if (is_array ($uidArr)) {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $otDa[0]['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr)) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            } else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $otDa[0]['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            }
                        }else{
                            continue;
                        }

                    }else{
                        if (is_array ($uidArr)) {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $otDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if (in_array ($performanceArrItem, $uidArr)) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        } else {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $otDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if ($performanceArrItem == $uidArr) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        }
                    }
                }

                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $buDa = $business->getWhereIdDataSelectModel($item['order_company_id'],$item['certificate_code']);
                if ($buDa) {
                    if($cerId == $this->erleiCertificate_id || empty($cerId)){
//                        //企业证书没有证书类别
                        if (is_array ($uidArr)) {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $buDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if (in_array ($performanceArrItem, $uidArr)) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }

                        } else {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $buDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if ($performanceArrItem == $uidArr) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }

                        }
                    }
                }
            }
        }
        return $tdPrice;
    }

    /**
     *  获取证书注册退单全部业绩
     */
    public function getZszcChargeback($meekStart,$meekEnd,$uidArr,$cerId='')
    {
        //查询退单金额
        //查询时间段退单业绩
        $orderDealChargebackModel = new OrderDealChargeback();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
//        $other = new OrderOtherCertificateBusiness();
//        $business = new OrderEnterpriseCertificateBusiness();
//        $systemCertificate = new SystemCertificate();
//        $certificateModel = new SystemCertificate();

        $tdData = $orderDealChargebackModel->getBetDateTypeModel($meekStart,$meekEnd);
        $tdPrice = 0;
        if ($tdData){
            foreach ($tdData as $item) {
                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $comDa = $company->getIdCertificateCodeFindModel($item['order_company_id'],$item['certificate_code']);
                if ($comDa){
                    //有没有证书无所谓 计算证书注册总业绩
                    $tdPrice += $item['performance'];
//                    print_r ($comDa);die;
                    //如果有证书 先判断证书是否相等
//                    $cid = $certificateModel->getWhereCertificateIdModel($comDa['certificate_leixing']);
//                    $cid = $comDa['certificate_id'][0]; //证书类型
//                    if ($cerId){
//                        if ($cid == $cerId){
//                            if(is_array ($uidArr)){
//                                //查看业绩归属人有几人
//                                $performanceArr = explode (',', $comDa['performance_uid']);
//                                $performanceCount = count ($performanceArr);
//                                foreach ($performanceArr as $performanceArrItem) {
//                                    if (in_array ($performanceArrItem, $uidArr, true)) {
//                                        $tdPrice += $item['performance'] / $performanceCount;
//                                    }
//                                }
//                            }else {
//                                //查看业绩归属人有几人
//                                $performanceArr = explode (',', $comDa['performance_uid']);
//                                $performanceCount = count ($performanceArr);
//                                foreach ($performanceArr as $performanceArrItem) {
//                                    if ($performanceArrItem == $uidArr) {
//                                        $tdPrice += $item['performance'] / $performanceCount;
//                                    }
//                                }
//                            }
//                        }else{
//                            continue;
//                        }
//                    }else{
//                        if(is_array ($uidArr)){
//                            //查看业绩归属人有几人
//                            $performanceArr = explode (',', $comDa['performance_uid']);
//                            $performanceCount = count ($performanceArr);
//                            foreach ($performanceArr as $performanceArrItem) {
//                                if (in_array ($performanceArrItem, $uidArr, true)) {
//                                    $tdPrice += $item['performance'] / $performanceCount;
//                                }
//                            }
//                        }else {
//                            //查看业绩归属人有几人
//                            $performanceArr = explode (',', $comDa['performance_uid']);
//                            $performanceCount = count ($performanceArr);
//                            foreach ($performanceArr as $performanceArrItem) {
//                                if ($performanceArrItem == $uidArr) {
//                                    $tdPrice += $item['performance'] / $performanceCount;
//                                }
//                            }
//                        }
//                    }
                }

                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $exDa = $exchange->getWhereIdDataSelectModel($item['order_company_id'],$item['certificate_code']);
                if ($exDa) {
                   $tdPrice += $item['performance'];
                }
            }
        }
        return $tdPrice;
    }
    public function getTypeChargeback($meekStart,$meekEnd,$uidArr,$type)
    {
        //查询退单金额
        //查询时间段退单业绩
        $orderDealChargebackModel = new OrderDealChargeback();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $other = new OrderOtherCertificateBusiness();
        $business = new OrderEnterpriseCertificateBusiness();
        $systemCertificate = new SystemCertificate();
        $certificateModel = new SystemCertificate();

        $tdData = $orderDealChargebackModel->getBetDateTypeModel($meekStart,$meekEnd);
        $tdPrice = 0;
        if ($tdData){
            foreach ($tdData as $item) {
                switch ($type){
                    case 1:
                        //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                        $performanceUid = $company->getOrderPerformanceUidModel($item['order_company_id'],$item['certificate_code']);
                        if($performanceUid){
                            if(is_array ($uidArr)){
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr, true)) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            }else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            }
                        }
                        break;
                    case 2:
                        //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                        $performanceUid = $exchange->getOrderPerformanceUidModel($item['order_company_id'],$item['certificate_code']);
                        if ($performanceUid){
                            if(is_array ($uidArr)){
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr, true)) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            }else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            }
                        }
                        break;
                    case 3:
                        //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                        $performanceUid = $other->getOrderPerformanceUidModel($item['order_company_id'],$item['certificate_code']);
                        if ($performanceUid){
                            if (is_array ($uidArr)) {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr, true)) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            } else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            }
                        }
                        break;
                    case 4:
                        //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                        $performanceUid = $business->getOrderPerformanceUidModel($item['order_company_id'],$item['certificate_code']);
                        if ($performanceUid){
                            if (is_array ($uidArr)) {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr, true)) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }

                            } else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }

                            }
                        }
                        break;
                }
            }
        }
        return $tdPrice;
    }

    /**
     * 获取退单退证业绩
     * @param $startDate
     * @param $endDate
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getChargrBack($startDate,$endDate,$id,$ylId,$elId)
    {
        $yl = 0;//一类业绩总和
        $el = 0;//二类业绩总和
        $orderChargeBack = new OrderDealChargeback();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $other = new OrderOtherCertificateBusiness();
        $business = new OrderEnterpriseCertificateBusiness();
        //查询时间端内退单内容
        $chargeData = Db::table('order_deal_chargeback')
            ->whereBetweenTime('create_date',$startDate,$endDate)
            ->where('is_complete',1)
            ->whereNull('delete_date')
            ->field('certificate_code,performance')
            ->select ();

        foreach ($chargeData as $chargeDatum) {
            //企业证书就是二类证书
            $businData = $business->getFinanceFieldFindModel ($chargeDatum['certificate_code']);

            if(!empty($businData)) {
                $uidArr = explode (',',$businData['performance_uid']);
                foreach ($uidArr as $uid) {
                    if($uid == $id){
                        $el += $chargeDatum['performance']/count($uidArr);
                    }
                }
            }
            //查询该证书是一类还是二类证书
            $comData = $company->getCertificateCodeFindModel ($chargeDatum['certificate_code']);

            if ($comData && $comData['performance_uid']) {
                //判断证书是一类还是二类
                $uidArr = explode (',',$comData['performance_uid']);

                if ($comData['certificate_id'][0] == $ylId){
                    foreach ($uidArr as $uid) {
                        if($uid == $id){
                            $yl += $chargeDatum['performance']/count($uidArr);
                        }
                    }
                }

                if ($comData['certificate_id'][0] == $elId){
                    foreach ($uidArr as $uid) {
                        if($uid == $id){
                            $el += $chargeDatum['performance']/count($uidArr);
                        }
                    }
                }

            }
            $exData = $exchange->getFinanceFieldFindModel ($chargeDatum['certificate_code']);
            if ($exData){
                //判断证书是一类还是二类
                $uidArr = explode (',',$exData['performance_uid']);
                if ($exData['certificate_leixing']['id'] == $ylId){
                    foreach ($uidArr as $uid) {
                        if($uid == $id){
                            $yl += $chargeDatum['performance']/count($uidArr);
                        }
                    }
                }

                if ($exData['certificate_leixing']['id'] == $elId){
                    foreach ($uidArr as $uid) {
                        if($uid == $id){
                            $el += $chargeDatum['performance']/count($uidArr);
                        }
                    }
                }
            }
            $othData = $other->getFinanceFieldFindModel ($chargeDatum['certificate_code']);
            if ($othData){
                //判断证书是一类还是二类
                $uidArr = $othData['performance_uid'];
                if ($othData['certificate_id'][0] == $ylId){
                        if($uidArr == $id){
                            $yl += $chargeDatum['performance'];
                        }
                }

                if ($othData['certificate_id'][0] == $elId){
                    if($uidArr == $id){
                        $el += $chargeDatum['performance'];
                    }
                }
            }
        }

        return ['yl'=>$yl,'el'=>$el];

    }
    /**
     * 获取对应字母
     * 1=>A 2=>B
     */
    public function getLetter($num): string
    {
        $y = ($num / 26);
        if ($y >= 1) {
            $y = (int)$y;
            $letter = chr($y+64);
        } else {
            $letter = chr($num+65);
        }

        return $letter;
    }

    /**
     * 手动更新某一天数据
     */
    public function update(Request $request,RedisService $redisService)
    {
        $date = $request->get('date');
        if (!$date) return ReJson(message::$ERROR_CODE,"发生错误");

        $redisService->updateDateBetweenData($date,$date);
    }


}